<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.IO;
using System.Net;
using System.Drawing;
using System.Configuration;
public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //Step 1: Get QueryString
        string url = "";
        int maxWidth = 300;
        int maxHeight = 800; // maxHeight phai lon hon chieu cao anh to nhat tren trang chuyen muc
        
        decimal dHeight, dWidth, dNewHeight, dNewWidth;

        url = context.Request.QueryString["xurl"];
        url = context.Server.UrlDecode(url);// Uri.UnescapeDataString(url);
        maxWidth = Convert.ToInt32(context.Request.QueryString["w"] == null ? maxWidth.ToString() : context.Request.QueryString["w"]);
            
        //Step 2: Load data image from url;
        HttpWebRequest wreq;
        HttpWebResponse wresp;
        Stream mystream;
        //context.Response.Write(HttpUtility.UrlDecode(url));
        //return;
        url = url.Replace("%25", "%2525");// dau %
        
        wreq = (HttpWebRequest)WebRequest.Create(url);
        wreq.AllowWriteStreamBuffering = true;
        wresp = (HttpWebResponse)wreq.GetResponse();
        
        System.Drawing.Image bmp = null;
        if ((mystream = wresp.GetResponseStream()) != null){
            bmp = new Bitmap(mystream);
        }
        
        //Step 3: Process resize image
        int width = bmp.Width;
        int height = bmp.Height;
        //only resize if the image is bigger than the max
        if (width > maxWidth)
        {
            Decimal divider = Math.Abs((Decimal)width / (Decimal)maxWidth);
            height = (int)Math.Round((Decimal)(height / divider));
        }
        else
        {
            maxWidth = width;
        }
        
        
        
        if (height > maxHeight)
        {
            Decimal divider = Math.Abs((Decimal)maxHeight * (Decimal)maxWidth);
            maxWidth = (int)Math.Round((Decimal)(divider / height));
            height = maxHeight;
            
        }

        Size newSize = new Size(maxWidth, height);

        using (Bitmap thumb = new Bitmap((System.Drawing.Image)bmp, newSize))
        {
            using (Graphics g = Graphics.FromImage(thumb)) // Create Graphics object from original Image
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                //Set Image codec of JPEG type, the index of JPEG codec is "1"
                System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1];

                //Set the parameters for defining the quality of the thumbnail... here it is set to 100%
                System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1);
                eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);

                //Now draw the image on the instance of thumbnail Bitmap object
                g.DrawImage(bmp, new Rectangle(0, 0, thumb.Width, thumb.Height));
            }

            MemoryStream objMemoryStream = new MemoryStream();
            thumb.Save(objMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] imageContent = new byte[objMemoryStream.Length];
            objMemoryStream.Position = 0;
            objMemoryStream.Read(imageContent, 0, (int)objMemoryStream.Length);
            context.Response.ContentType = "image/jpeg";
            context.Response.BinaryWrite(imageContent);
        }
    }
       
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}